<?php

class User extends CI_Model {

	function login($username, $password) {
		$query = $this->db->get_where('user', array('username' => $username, 'password' => md5($password), 'level' => ALUMNI_TYPE ,'aktif' => ACTIVE_ACCOUNT));
		return $query->row_array();
	}
	
	function get_user($id = NULL){
		$this->db->select('*');
		$this->db->from('user');
		if($id != NULL){
			$this->db->where('user.id_user', $id);
		}
		$query = $this->db->get();
		return $query->result_array();	
	}
	
	function get_alumni($id){
		$this->db->select('*');
		$this->db->from('user');
		$this->db->join('alumni','alumni.id_user=user.id_user','left');
		$this->db->where('user.id_user', $id);
		$query = $this->db->get();
		return $query->result_array();		
	}
	
	function edit($id, $data){
		$this->db->where('id_user', $id);
		return $this->db->update('user',$data);
	}
	
	function edit_alumni($id, $data){
		$this->db->where('id_user', $id);
		return $this->db->update('alumni',$data);
	}
	
	function add($data){
		$this->db->insert('user',$data);
		return $this->db->insert_id();
	}
	
	function add_alumni($data){
		$this->db->insert('alumni', $data);
	}
	
	function check_user_exist($usr, $id = NULL){
		if($id != NULL){
			$this->db->where('id_user !=', $id);
		}
		$this->db->where('username',$usr);
		$query=$this->db->get('user');
		if($query->num_rows()>0) {		
			return true;		
		}else{ 
			return false;	
		}
	}
	
	function delete($id){
		$this->db->delete('user', array('id_user' => $id)); 
	}
	
	function delete_alumni($id){
		$this->db->delete('alumni', array('id_user' => $id)); 
	}
	
}